
//shareCount分享次数
//formShare从分享打开次数

class DataCollection {
  reset(dataCollection:any) {
    dataCollection.enterTime = 0
    dataCollection.leaveTime = 0
    dataCollection.remainTime = 0
    dataCollection.platform = ''
    dataCollection.model = ''
    dataCollection.userInfo = {}
    dataCollection.userLocation = ''
    dataCollection.city.clear()
    dataCollection.region.clear()
    dataCollection.class.clear()
    dataCollection.product.clear()
    dataCollection.site.clear()
    dataCollection.page.clear()

  }
  //设置map
  setMapData(key: string, value: any) {
    let app = getApp()
    let i = app.globalData.dataCollection[key].get(value) || 1
    if (app.globalData.dataCollection[key].has(value)) {
      i++
    }
    app.globalData.dataCollection[key].set(value, i)
  }
  //设置页面Map mode = enter | leave 记录打开页面次数 打开时间 离开时间 停留时间
  setPageMapData(mode: string, pageTitle: string = '') {
    let app = getApp()
    const routes = getCurrentPages()
    let value = routes[routes.length - 1].route
    let obj = app.globalData.dataCollection.page.get(value) || {
      count: 1,
      title: '',
      list: [
        {
          enterTime: 0,
          leaveTime: 0,
          remainTime: 0,
        }
      ]
    }

    if (mode === 'enter') {
      //进入页面
      if (app.globalData.dataCollection.page.has(value)) {
        obj.count++
        obj.list.push({
          enterTime: 0,
          leaveTime: 0,
          remainTime: 0,
        })
      }
      obj.title = pageTitle
      obj.list[obj.count - 1].enterTime = new Date().getTime()

    } else if (mode === 'leave') {
      //离开页面
      obj.list[obj.count - 1].leaveTime = new Date().getTime()
      obj.list[obj.count - 1].remainTime = obj.list[obj.count - 1].leaveTime - obj.list[obj.count - 1].enterTime
    }
    app.globalData.dataCollection.page.set(value, obj)
  }
  //记录打开的班别|学车类型 Map mode = enter | leave 记录打开页面次数 打开时间 离开时间 停留时间
  //type: class | product | site
  setComprehensiveMapData(key: string, mode: string, type: string) {
    let app = getApp()
    let obj = app.globalData.dataCollection[type].get(key) || {
      count: 1,
      list: [
        {
          enterTime: 0,
          leaveTime: 0,
          remainTime: 0,
        }
      ]
    }

    if (mode === 'enter') {
      //进入页面
      if (app.globalData.dataCollection[type].has(key)) {
        obj.count++
        obj.list.push({
          enterTime: 0,
          leaveTime: 0,
          remainTime: 0,
        })
      }
      obj.list[obj.count - 1].enterTime = new Date().getTime()

    } else if (mode === 'leave') {
      //离开页面
      obj.list[obj.count - 1].leaveTime = new Date().getTime()
      obj.list[obj.count - 1].remainTime = obj.list[obj.count - 1].leaveTime - obj.list[obj.count - 1].enterTime
    }
    app.globalData.dataCollection[type].set(key, obj)
  }
  //点击了按钮事件 企业客服icon、客服电话、支付按钮、导航按钮
  /**
   * {
   *   type:'类型',
   *   name:'事件名称',
   *   list:[{ time:'时间'}]
   * }
   */
  setButtonEventMapData(){}
}

export const dataCollection = new DataCollection()
